package src.com.qf.dao.impl;


import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import src.com.qf.dao.AddressDao;
import src.com.qf.entity.Address;
import src.com.qf.utils.JdbcUtil;

import java.sql.SQLException;
import java.util.List;

public class AddressDaoImpl implements AddressDao {
    QueryRunner queryRunner = new QueryRunner();
    @Override
    public List<Address> selectAddressByUid(Integer uid) {
        String sql = "select *from tb_address where uid = ?";

        List<Address> query = null;
        try {
            query = queryRunner.query(JdbcUtil.getCon(), sql, new BeanListHandler<>(Address.class), uid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query;
    }

    @Override
    public void updateLevelByUid(Integer uid) throws SQLException {
        String sql = "update tb_address set level = 1 where uid=?";
        queryRunner.update(JdbcUtil.getCon(),sql,uid);
    }
    @Override
    public int updateLevelById(Integer id)  {
        String sql = "update tb_address set level = 0 where id=?";
        int result = 0;
        try {
            result = queryRunner.update(JdbcUtil.getCon(),sql,id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public int insertAddress(String strName, String strPhone, String strDetail, Integer uid) {
        String sql = "insert tb_address(name,phone,detail,uid,level) values (?,?,?,?,1)";
        int update = 0;
        try {
            update = queryRunner.update(JdbcUtil.getCon(), sql, strName, strPhone, strDetail, uid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return update;
    }

    @Override
    public int deleteAddressById(String strId, Integer uid) {
        String sql = "delete from tb_address where id =? and uid = ?";
        int result = 0;
        try {
            result = queryRunner.update(JdbcUtil.getCon(),sql,strId,uid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public int updateAddressById(String strName, String strPhone, String strDetail, Integer id, Integer uid) {
        String sql = "update tb_address set name=? ,phone=? ,detail=? where id =? and uid =?";
        int result = 0;
        try {
            result = queryRunner.update(JdbcUtil.getCon(),sql,strName,strPhone,strDetail,id,uid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public Address selectAddressById(Integer aid) {
        String sql = "select *from tb_address where id = ?";

        Address query = null;
        try {
            query = queryRunner.query(JdbcUtil.getCon(), sql, new BeanHandler<>(Address.class), aid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return query;
    }
}
